package com.fxd.util;

import com.fxd.conn.JedisConnUtil;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;

/**
 * 分布式Id工具类
 */
public class IdGenerateUtil {

    private IdGenerateUtil() {
    }

    /**
     * 获取分布式Id，每次+1
     * 8位数，不够用0填充
     *
     * @param key 主键
     * @return
     */
    public static String getDCSId(String key) {
        RedisAtomicInteger redisAtomicInteger = new RedisAtomicInteger(key, JedisConnUtil.redisConnectionFactory);
        return String.format("%08d", redisAtomicInteger.incrementAndGet());
    }

    /**
     * 指定每次+？
     *
     * @param key
     * @param step
     * @return
     */
    public static String getDCSIdByStep(String key, Integer step) {
        RedisAtomicInteger redisAtomicInteger = new RedisAtomicInteger(key, JedisConnUtil.redisConnectionFactory);
        return String.format("%08d", redisAtomicInteger.addAndGet(step));
    }
}
